package com.filetransfer.viewmodel.base;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.Vector;
@Slf4j
@EqualsAndHashCode(callSuper = false)
public abstract class TaskTableRowModel extends Vector<Object> {
    @Nullable
    private TaskTableModel table;
    protected TaskTableRowModel(Object... cells) {
        super(cells.length);// 初始化Vector的大小
        this.addAll(Arrays.asList(cells));// 添加元素（对应一行的数据）
    }
    protected void setProperty(int columnIndex, Object value) {
        set(columnIndex, value);// set方法是Vector的方法
        if (table != null) {// 如果绑定了表格，更新表格，实现视图更新
            var row = getRowInTable();
            if (row != -1)
                table.fireTableCellUpdated(row, columnIndex);
        }
    }
    public int getRowInTable() {
        if (table != null) {
            var index = 0;
            for (var vector : table.getDataVector()) {
                if (vector == this) {// 通过引用判断是否是同一个对象
                    return index;
                }
                index++;
            }
        }
        return -1;
    }
    /**
     * 绑定到表格
     */
    public void bindToTable(TaskTableModel taskTableModel) {
        taskTableModel.addRow(this);
        table = taskTableModel;
    }
}
